import Vue from 'vue'
import Router from 'vue-router'
import 'nprogress/nprogress.css'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  // 新增月卡管理页
  {
    path: '/cardAdd',
    component: () => import('@/views/nested/monthlyCard/add-card.vue')
  },
  // 新增企业管理页
  {
    path: '/exterpriseAdd',
    component: () => import('@/views/enterprise/add.vue')
  },
  // 查看企业页
  // 使用params传递id必须在路由配置时添加占位符，即/:id
  // 使用params: $router.push(`/exterpriseDetail/${row.id}`
  // 使用query传参id不用添加配置符（但是这样在浏览器网址上会显示id=）
  // 使用query: $router.push(`/exterpriseDetail/id=${row.id}`
  {
    path: '/exterpriseDetail/:id',
    component: () => import('@/views/enterprise/detail.vue')
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'Dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: { title: '工作台', icon: 'dashboard' }
    }]
  },

  {
    path: '/example',
    component: Layout,
    redirect: '/example/building',
    name: 'Example',
    meta: { title: '园区管理', icon: 'el-icon-s-help' },
    children: [
      {
        path: 'building',
        name: 'Table',
        component: () => import('@/views/building/index'),
        meta: { title: '楼宇管理', icon: 'building' }
      },
      {
        path: 'enterprise',
        name: 'Tree',
        component: () => import('@/views/enterprise/index'),
        meta: { title: '企业管理', icon: 'enterprise' }
      }
    ]
  },

  {
    path: '/form',
    component: Layout,
    redirect: '/Form/MonthlyCard',
    meta: { title: '行车管理', icon: 'form' },
    children: [
      {
        path: 'index',
        name: 'Form',
        component: () => import('@/views/form/index')
      },
      {
        path: 'monthlyCard',
        component: () => import('@/views/nested/monthlyCard'),
        name: '月卡管理',
        meta: { title: '月卡管理' }
      },
      {
        path: 'charging',
        component: () => import('@/views/nested/charging'),
        name: '计费规则管理',
        meta: { title: '计费规则管理' }
      }
    ]
  },
  {
    path: '/steelyard',
    component: Layout,
    redirect: '/steelyard/steelyard1',
    name: 'steelyard',
    meta: {
      title: '一体杆管理',
      icon: 'nested'
    },
    children: [
      {
        path: 'steelyard1',
        name: 'steelyard1',
        component: () => import('@/views/steelyard/steelyard1/index.vue'),
        meta: {
          title: '一体杆管理'
        }
      },
      {
        path: 'alarm',
        name: 'alarm',
        component: () => import('@/views/steelyard/alarm/index.vue'),
        meta: {
          title: '告警记录'
        }
      }
    ]
  },

  {
    path: '/system',
    component: Layout,
    redirect: '/system/role',
    name: 'system',
    meta: { title: '系统管理', icon: 'link' },
    children: [
      {
        path: 'role',
        name: 'role',
        component: () => import('@/views/system/role/index.vue'),
        meta: { title: '角色管理' }
      },
      {
        path: 'staff',
        name: 'staff',
        component: () => import('@/views/system/staff/index.vue'),
        meta: { title: '员工管理' }
      }
    ]
  },

  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}
export default router
